package kr.hkit.powerjava.chapter14;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Cards extends JPanel{
	CardLayout layout;
	public Cards() {
		layout=new CardLayout();
		setLayout(layout);
		for(int i=1;i<=10;i++){
			add(new JButton("현재카드번호"+i+"입니다."));//버튼이 여러개 포개어져 있음
		}
	}
}
class CardFrame extends JFrame{
	Cards card;
	public CardFrame(String title){
		super(title);
		initialize();
	}

	private void initialize() {
		setSize(400, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel= new JPanel(new GridLayout(0,5,10,0));
		addButton("<<",panel);
		addButton("<",panel);
		addButton(">",panel);
		addButton(">>",panel);
		addButton("종료",panel);
		
		add(panel,BorderLayout.NORTH);
		
		card= new Cards();add(card,BorderLayout.CENTER);
		setVisible(true);
	}

	private void addButton(String text, JPanel panel) {
		JButton btn= new JButton(text);
		btn.addActionListener(listener);
		panel.add(btn);
	}
	ActionListener listener=new ActionListener() {//필드로 선언
		
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("getActionCommand()"+e.getActionCommand());
			System.out.println("getSource()"+e.getSource());
			System.out.println("getID()"+e.getID());
			if(e.getActionCommand().equals("종료")){
				System.exit(0);
			}else if(e.getActionCommand().equals("<<")){
				card.layout.first(card);
			}else if(e.getActionCommand().equals("<")){
				card.layout.previous(card);
			}else if(e.getActionCommand().equals(">")){
				card.layout.next(card);
			}else 
				card.layout.last(card);
		}
	};
}
public class CardLayoutEx {
	public static void main(String[] args) {
		new CardFrame("CardLayout 예제");
	}
}
